package top.gcl.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import top.gcl.entities.Notice;
import top.gcl.json.JsonResult;

import javax.annotation.Resource;

@RestController
@Slf4j
public class FollowController {
    public static final String NOTICE_URL = "http://localhost:8001";
    @Resource
    private RestTemplate restTemplate;

    @PostMapping(value = "/follow")
    public JsonResult follow(Notice notice) {
        return restTemplate.postForObject(NOTICE_URL + "/sendNotice/", notice, JsonResult.class);
    }

    @GetMapping(value = "/getNotice/{id}")
    public JsonResult getNotice(@PathVariable("id") String id) {
        return restTemplate.getForObject(NOTICE_URL + "/getNoticeById/" + id, JsonResult.class);
    }


    @GetMapping(value = "/getNotice2/{id}")
    public JsonResult getNotice2(@PathVariable("id") String id) {
        log.info("---" + id);
        ResponseEntity<JsonResult> re = restTemplate.getForEntity(NOTICE_URL + "/getNoticeById/{1}", JsonResult.class, id);
        if (re.getStatusCode().is2xxSuccessful()) {
            return re.getBody();
        } else {
            return new JsonResult(-1, "失败！");
        }
    }
}